<script type="text/javascript">
var paginationTotal = 1;//记录总数
var sortColumn = "";
var sortMethod = "";
$(function(){
//	initData(0);
	$("#confirmDialog").dialog({
		autoOpen:false,
		modal:true,
		show:"slide",
		buttons:{
			"<{$lang.ok}>":function(){
				$(this).dialog("close");				
			}
		}
	});
	
	$(".search").click(function(){
		initData(0);
	});
	
	$(".toEnter").keyup(function(e){
		var key = e.which;
		if(key==13){
			initData(0);
		}
	});
	
	
	$("#ckAll").click(function(){
		$("[name=checked]").attr("checked",$(this).attr("checked")=="checked"?true:false);
	});
	
	$("#SwichBrowseType").click(function(){
		if($(this).attr("status") == "0"){
			$(".subTabTr").show();
			$(this).attr("status","1");
		}else{
			$(".subTabTr").hide();
			$(this).attr("status","0");
		}
	});
	
	$(".sort").click(function(){
		sortColumn = $(this).attr("sortColumn");
		sortMethod = $(this).attr("sortMethod");
		$(".sort").children(".up").remove();
		if(sortMethod == 'asc'){
			$(this).attr("sortMethod","desc");
			$(this).append("<span class='up'>↑</span>");
		}else{
			$(this).attr("sortMethod","asc");
			$(this).append("<span class='up'>↓</span>");
		}
		
		initData(paginationCurrentPage-1);
	});

    $(".viewdetail").live("click",function(){
        var receiving_code = $(this).attr("receivingcode");
        window.open("/product/returns/detail/code/"+receiving_code+"/view/1" );
    });

	$(".detail").live("click",function(){
		var receiving_code = $(this).attr("receivingcode");
		$.ajax({
			url: "/product/returns/package-proccess-check",
			async: false,
       	 	dataType: "json",
			type:"post",
			data:{'receiving_code':receiving_code},
			success:function(json){
				if(json.ask==1){
					var tip = '';			
					$.each(json.msg,function(key,val){
						tip += val;
					});
					initData(0);
					$("#confirmDialog").html(tip).dialog('open');
				}else {
					window.open("/product/returns/detail/code/"+receiving_code );
				}				
			}			
		});		
	});
	
	$(".receive").live("click",function(){
		var obj = this;
		$.ajax({
			url:"/product/returns/check-has-first/code/"+$(this).attr("receivingcode"),
			dataType:"json",
			success:function(json){
				if(json){
					$("#confirmDialog").dialog("option","buttons",[{
						text:"<{$lang.yes}>",
						click:function(){
							window.open("/product/returns/receive/code/"+$(obj).attr("receivingcode"));
							$(this).dialog("close");
						}
					},{
						text:"<{$lang.no}>",
						click:function(){
							$(this).dialog("close");
						}
					}]).html("<{$lang.first}><{$lang.receive}>?").dialog("open");
					
				}else{
					window.open("/product/returns/receive/code/"+$(obj).attr("receivingcode"));
				}
			}
		});
		
	});

});

function loadData(page, pageSize){
    loading();
	var receivingCode = $("[name=receivingCode]").val();
	var status = $("[name=status]").val();
	var refId = $("[name=refId]").val();
	var customerId = $("[name=customerId]").val();
    $.ajax({
		url: "/product/returns/load-list/page/" + page + "/pageSize/" + pageSize,
        async: false,
        dataType: "json",
		type:"post",
		data:{"receivingCode":receivingCode,"status":status,"refId":refId,"customerId":customerId,"sortColumn":sortColumn,"sortMethod":sortMethod},
        success: function(json) {
            var html = "";
            var view='';
			if(json.ask){
				paginationTotal = json.total;
				var i = (page == 1 ? 1 : pageSize * (page - 1) + 1);
				$.each(json.result,function(k,v){
                    view= '&nbsp;&nbsp;<img src="/images/icon/info.jpg" title="<{$lang.print}><{$lang.items}>" style="cursor:pointer" receivingcode="'+v.receiving_code+'" class="viewdetail"/>';
					html += '<tr><td>'+(i++)+'</td><td class="showDetail" detailid="'+v.receiving_id+'">'+v.receiving_code+view+'</td><td>'+v.customer_id+'</td><td>'+v.statusText+'</td><td>'+v.ref_id+'</td><td>'+v.orderTypeText+'</td><td>'+v.created+'</td><td>'+v.description+'</td><td>'+v.unNormal+'</td><td>';
					 if(v.status == 1){
						html += '<img src="/images/icon/printer.gif" title="<{$lang.print}><{$lang.items}>" style="cursor:pointer" receivingcode="'+v.receiving_code+'" class="detail"/>&nbsp;&nbsp;';
					 }
					 if(v.status > 0 && v.status< 3){
						html += '<img src="/images/icon/edit.gif" title="Receive" style="cursor:pointer" receivingcode="'+v.receiving_code+'" class="receive"/>';
					 }
					html += '</td></tr>';
				});
				$("#loadData").html(html);
				$(".myTab tr:even").addClass("even-tr");
			}else{
				$("#loadData").html("<tr><td colspan='10' align='center'><{$lang.no_data}></td></tr>");
			}
        }
    });
}

function showItem(){
	$('#itemSubmit').show();
}

function addItem(){
	var orders_code = $('[name=orders_code]').val(); 
	if(!orders_code){
		$("#confirmDialog").html("<{$lang.orders_code}><{$lang.can_not_be_empty}>").dialog("open");
		return false;
	}
	$.ajax({
		type:'post',
		async: false,
		dataType: "json",
		url: "/product/returns/rma-create",
		data:{"orders_code":orders_code},
		success: function(json) {
			var tip = '';
			initData(0);
			$.each(json.msg,function(key,val){
				tip += json.receiving_code+' '+val;
			});
									
			$("#confirmDialog").html(tip).dialog("open");
			clear();
			$('#itemSubmit').hide();
		}
	});
	
}

function clear(){
	$('[name=orders_code]').val(''); 
}

</script>
<link href="/css/product/receiving.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
.myTab table td{ border:0;text-align:center;}
.subTabTr{display:none;}
.subTab{width:100%;font-style:italic;background:white; }
.subTab tr:hover{}
.subTab .subTabHeader{font-weight:800;background:#cccccc}
.subTab .subTabHeader:hover{font-weight:800;background:#cccccc}
.sort{cursor:pointer;}
.up{font-size:15px}

div.addinfo{
	margin-bottom:10px;
}
</style>
<div class="search_div">
    <form id="sForm" name="sForm">
		<div>
		<label>
			<{$lang.receive}><{$lang.code}>:<input type="text" name="receivingCode" class="myInput toEnter" />
		</label>&nbsp;
		<label>
			<{$lang.referNo}>:<input type="text" name="refId" class="myInput toEnter" />
		</label>&nbsp;
		<label>
			<{$lang.customer_code}>.:<input type="text" name="customerId" class="myInput toEnter" />
		</label>&nbsp;
		<label>
			<{$lang.status}>:<select name="status">
				<option value=""><{$lang.pls}></option>
				<{foreach key=key item=item from=$status}>
				<option value="<{$key}>"><{$item}></option>
				<{/foreach}>
			</select>
		</label>&nbsp;
		<input class="button search" type="button" value=" <{$lang.search}> ">&nbsp;&nbsp;
		</div>
	</form>
</div>

<div class="addinfo">
	<div>
		<input class="button" type="button" value="<{$lang.create}><{$lang.return_goods}><{$lang.code}>" onclick="showItem()" />
	</div>
	<div id="itemSubmit" style="display:none;">
		<{$lang.orders_code}>:<input type="text" name="orders_code" class="myInput" />	&nbsp;&nbsp;
		<input class="button" type="button" name="addRma" value=" <{$lang.submit}> " onclick="addItem()" />
	</div>
	<div id="itemmsg" style="color:red;"></div>
	
</div>

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="myTab">
    <tbody>
    <tr class="even-tr">
    	<th><{$lang.No}></th>
        <th class="sort" sortColumn="receiving_code" sortMethod="asc"><{$lang.receive}><{$lang.code}></th>
        <th class="sort" sortColumn="customer_id" sortMethod="asc"><{$lang.user}></th>
        <th><{$lang.status}></th>
        <th><{$lang.referNo}></th>
        <th><{$lang.order}><{$lang.type}></th>
        <th class="sort" sortColumn="created" sortMethod="asc"><{$lang.create}><{$lang.date}></th>
        <th><{$lang.remark}></th>
        <th><{$lang.exception}></th>
        <th><{$lang.operation}></th>
    </tr>
    </tbody>
    <tbody id="loadData"></tbody>
</table>
<div class="pagination"></div>

<div id="confirmDialog" title="<{$lang.system}><{$lang.msg}>">
	
</div>